package search

import (
	"github.com/gin-gonic/gin"
	"moments/global"
	"moments/handle/model"
	"moments/handle/request"
	"net/http"
)

// 好友添加
func AddFriend(c *gin.Context) {
	var form request.AddFriend
	if err := c.ShouldBind(&form); err != nil {
		c.JSONP(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "参数错误",
		})
		return
	}

	var friend model.FriendType

	friend = model.FriendType{
		UserId:   friend.UserId,
		FriendId: friend.FriendId,
		Content:  friend.Content,
		Status:   friend.Status,
	}

	if err := friend.AddFriend(global.DB); err != nil {
		c.JSONP(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "好友添加失败",
		})
		return
	}

	c.JSONP(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "好友添加成功",
	})
}

// 好友列表
func ListFriend(c *gin.Context) {
	var form request.List
	if err := c.ShouldBind(&form); err != nil {
		c.JSONP(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "参数错误",
		})
		return
	}

	var friend model.FriendType
	listFriend, err := friend.ListFriend(global.DB, form.UserId)
	if err != nil {
		c.JSONP(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "查询失败",
		})
		return
	}

	c.JSONP(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "好友查询成功",
		"Data": map[string]interface{}{
			"list": listFriend,
		},
	})

}
